<template>
  <div class="mod-config">
    <basic-container>
      <el-divider content-position="left">查询条件</el-divider>
      <el-form inline size="mini" label-width="80px">
        <el-row>
          <el-col :span="12"  v-if="[1,21,23].includes(userInfo.lessDeptId)">
            <el-form-item label="仓位名称">
              <el-input size="mini" type="textarea" v-model="dataForm.stockName" readonly>
              </el-input>
            </el-form-item>
            <el-form-item label-width="0">
              <el-button size="mini" @click="getStockList">选择</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12"  v-else>
            <el-form-item label="仓位名称">
              <el-input size="mini" type="textarea" v-model="dataForm.stockName" disabled>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="查看类型">
              <el-radio v-model="dataForm.statisticalType" label="1">按商品类型</el-radio>
              <el-radio v-model="dataForm.statisticalType" label="2">按品种</el-radio>
              <el-radio v-model="dataForm.statisticalType" label="3">按制造商</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品类别">
              <el-select v-model="dataForm.ctgId" @change="changeCtgId" clearable>
                <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制造商">
              <el-input v-model="dataForm.supplyName" @change="changeSupplyId">
                <el-button slot="append" @click="openSupplyDialog">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品品种">
              <el-input v-model="dataForm.brandName" readonly="readonly">
                <el-button slot="append" @click="getBrandList">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品种名称">
              <el-input size="mini" v-model="dataForm.likeBrandName" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 10px;">
          <el-form-item label-width="5px">
            <el-link :href="printUrl" target="_blank" type="primary" :disabled="isDisabled">
              <el-button size="mini" type="primary" @click="getAllocationInfoList">查 询</el-button>
            </el-link>
          </el-form-item>
        </el-row>
      </el-form>
      <search-stock v-if="stockVisible" ref="stockInfo" @refreshData="getStockInfoList"></search-stock>
      <!-- 弹窗，查询制造商 -->
      <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
      <!-- 弹窗, 查询商品品种-->
      <search-brand v-if="brandVisible" ref="selectBrand" @refreshData="getBrandData"></search-brand>
    </basic-container>
  </div>
</template>

<script>
let params = "";
import { filterParams, getDateFormat } from "@/util/tools"
import { mapGetters } from "Vuex"
import { getUserInfo } from "@/api/admin/user"
import { fetchStockList,getStockInfo } from '@/api/eims/stock'
import searchStock from "@/components/business/stock-search"
import { getCtgList } from '@/api/eims/ctg'

import SearchSupply from '@/components/business/SearchSupply'
import SearchBrand from '@/components/business/SearchBrand'


export default {
  data() {
    return {
      printUrl: "",
      isDisabled: true,
      outStockId: "",
      inStockId: "",
      dataForm: {
        receiveTimeStart: "",
        receiveTimeEnd: "",
        type: "1",
        statisticalType: "1",
        goodsCode: "",
        goodsName: "",
        priceType: "1",
        stockName: "",
        stockId: [],
        likeBrandName:""
      },
      deptOptions: [],
      stockOptions: [],
      stockVisible: false,
      supplyVisible: false,
      brandVisible: false,
      ctgOptions: [],
    };
  },
  computed: {
    ...mapGetters(["userInfo", "bizData"])
  },
  components: {
    searchStock,
      SearchSupply,
      SearchBrand
  },
  created() {
    this.getStockOption();
    this.getGoodsCtgList();
    this.getStockName();
  },
  methods: {
      getStockName(){
          let deptId = this.userInfo.lessDeptId
          getStockInfo(deptId).then(result=>{
              this.dataForm.stockName = result.data.data[0].name;
          })
      },
    typeChangeHandle() {
      this.outStockId = ''
      this.inStockId = ''
      this.dataForm.outStockIds = []
      this.dataForm.outStockName = ''
      this.dataForm.inStockIds = []
      this.dataForm.inStockName = ''
    },
    outStockChangeHandle(val) {
      this.stockOptions.forEach(item => {
        this.dataForm.outStockIds = []
        if(item.id == val){
          this.dataForm.outStockName = item.name
        }

      })
        this.dataForm.outStockIds.push(val)
    },
    inStockChangeHandle(val) {
      this.stockOptions.forEach(item => {
        this.dataForm.inStockIds = []
        if(item.id == val){
          this.dataForm.inStockName = item.name
        }

      })
        this.dataForm.inStockIds.push(val)
    },
      changeCtgId(){

      },
    getAllocationInfoList() {
      this.isDisabled = false
        if(this.dataForm.statisticalType ==1){
          params = "/storage/storage_kccpsljetjtype.cpt";
        }else if(this.dataForm.statisticalType ==2){
          params = "/storage/storage_kccpsljetj.cpt";
        }else if(this.dataForm.statisticalType ==3){
          params = "/storage/storage_kccpsljetjpp.cpt";
      }
      let data = filterParams(this.dataForm);
      let parameter = "";
      for (let key in data) {
        parameter += "&" + key + "=" + this.dataForm[key];
      }
      //let frUrl = "http://localhost:8075/webroot/decision/view/report?viewlet="
      //this.printUrl = frUrl + params + parameter
      this.printUrl = this.bizData.printUrl + params + parameter;

    },
    //根据工号获取员工真实名称
    getRealName(workNumber) {
      if (workNumber == "") {
        this.dataForm.createUserName = "";
        this.dataForm.createUser = "";
      } else {
        getUserInfo(workNumber).then(result => {
          if (result.data.data) {
            this.dataForm.createUserName = result.data.data.realName;
            this.dataForm.createUser = result.data.data.id;
          } else {
            this.$message.warning("请输入正确的员工工号");
          }
        });
      }
    },
    //  获取商品类别
    getGoodsCtgList() {
        getCtgList().then(result => {
            this.ctgOptions = result.data.data
        })
    },
    changeSupplyId(){
        this.dataForm.brandId = ''
        this.dataForm.brandName= ''
    },
    //  打开选择制造商弹窗
    openSupplyDialog() {
        if (!this.dataForm.ctgId) {
            this.$message.warning('请选择商品类别')
            return false
        }
        let data = {
            ctgId: this.dataForm.ctgId
        }
        this.supplyVisible = true
        this.$nextTick(() => {
            this.$refs.supplyDialog.init(data)
        })
    },
    getSupplyData(data){
        this.$set(this.dataForm,'supplyId',data.id)
        this.$set(this.dataForm,'supplyName',data.name)
    },
    // 获取商品品种信息
    getBrandList() {
        if (!this.dataForm.ctgId) {
            this.$message.warning('请选择商品类别')
            return false
        }
        if (!this.dataForm.supplyId) {
            this.$message.warning('请选择制造商')
            return false
        }
        let data = {
            ctgId: this.dataForm.ctgId,
            supplyId: this.dataForm.supplyId
        }
        this.brandVisible = true
        this.$nextTick(() => {
            this.$refs.selectBrand.init(data)
        })
    },
      getBrandData(data){
        this.$set(this.dataForm,'brandId',data.id)
          this.$set(this.dataForm,'brandName',data.name)
      },
    getStockList() {
      this.stockVisible = true;
      let data = [];
      if (this.dataForm.stockId) {
        data = this.dataForm.stockId;
      }
      this.$nextTick(() => {
        this.$refs.stockInfo.init(data);
      });
    },
    getStockInfoList(data) {
      let name = "";
      this.dataForm.stockId = []
        console.log(data)
      data.forEach(result => {
        this.dataForm.stockId.push(result.id)
        name = name ? name + "," + result.name : result.name;
      });
      this.dataForm.stockName = name
    },
    getStockOption(){
      fetchStockList().then(result => {
        this.stockOptions = result.data.data.records
      })
    }
  }
};
</script>

<style lang="scss" scoped>
.row-bg {
  background-color: #cadee8;
  // padding-left: 15px;
  padding-right: 15px;
  padding-top: 5px;
  margin-bottom: 2px;
}
</style>
